<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Categories.php 130 2009-05-21 14:23:24Z movoin $
 */

/**
 * 分类显示控件
 *
 */
function _ctlCategories($name, $args=null)
{
    $opts = array('rows', 'selected');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);

    $output = '<select id="' . $name . '" name="' . $name . '">';
    if ($data['rows']) {
        foreach ($data['rows'] as $row) {
            if ($row['child']) {
                $output .= '<optgroup label="' . $row['name'] . '">';
                foreach ($row['child'] as $child) {
                    if ($data['selected']==$child['cate_id']) {
                        $selected = ' selected="selected"';
                    } else {
                        $selected = '';
                    }
                    $output .= '<option value="' . $child['cate_id'] . '"' . $selected . '>' . $child['name'] . '</option>';
                }
                $output .= '</optgroup>';
            } else {
                if ($data['selected']==$row['cate_id']) {
                    $selected = ' selected="selected"';
                } else {
                    $selected = '';
                }
                $output .= '<option value="' . $row['cate_id'] . '"' . $selected . '>' . $row['name'] . '</option>';
            }
        }
    }
    $output .= '</select>';

    return $output;
}
